package com.imonsoft.sqm.service.excel.IpicTargetExcel;

import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.imonsoft.common.util.StringUtil;
import com.imonsoft.common.util.ValidatorUtil;
import com.imonsoft.sqm.pojo.basedata.IpicTarget;
import com.imonsoft.sqm.pojo.basedata.Supplier;
import com.imonsoft.sqm.service.basedata.IpicTargetService;
import com.imonsoft.sqm.service.basedata.SupplierService;
import com.imonsoft.sqm.web.excel.IpicTargetExcel.IpicTargetImportDto;

@Service("ipicTargetExcelService")
public class IpicTargetExcelServiceImpl implements IpicTargetExcelService {
	@Autowired private SupplierService supplierService;
	@Autowired private IpicTargetService ipicTargetService;
	@Override
	public List<String> txCheckData(IpicTargetImportDto ipicTargetImportDto, List<IpicTargetImportDto> repeat) {
		List<String> messages = new ArrayList<String>();
		List<Supplier> supplierList = supplierService.queryAll();
		List<IpicTarget> ipicTargetList = ipicTargetService.queryAll();
		if (supplierList == null || supplierList.size() <= 0) {
			messages.add("系统中不存在供应商，请先维护供应商数据");
		}
		if (StringUtils.isEmpty(ipicTargetImportDto.getSupplierCode())) {
			messages.add("第[" + ipicTargetImportDto.getRow() + "]行供应商编号不能为空!");
		} else {
			if (supplierList != null && supplierList.size() > 0) {
				for (Supplier s : supplierList) {
					if (s.getCode().equals(ipicTargetImportDto.getSupplierCode())) {
						ipicTargetImportDto.setSupplierId(s.getId());
						ipicTargetImportDto.setSupplierName(s.getName());
						break;
					}
				}
			}
			if (ipicTargetImportDto.getSupplierId() == null || ipicTargetImportDto.getSupplierName() == null) {
				messages.add("第[" + ipicTargetImportDto.getRow() + "]行供应商系统中不存在!");
			}
		}
		if (StringUtils.isEmpty(ipicTargetImportDto.getYear())) {
			messages.add("第[" + ipicTargetImportDto.getRow() + "]行年份不能为空!");
		} else if (!ValidatorUtil.isNumber(ipicTargetImportDto.getYear())) {
			messages.add("第[" + ipicTargetImportDto.getRow() + "]行请输入正确的年份!");
		} else if (StringUtil.toLong(ipicTargetImportDto.getYear()) <= 0) {
			messages.add("第[" + ipicTargetImportDto.getRow() + "]行请输入正确的年份!");
		}
		if (StringUtils.isEmpty(StringUtil.fixup(ipicTargetImportDto.getValue()))) {
			messages.add("第[" + ipicTargetImportDto.getRow() + "]行目标值不能为空!");
		}else if (!ValidatorUtil.isDecimalLow(StringUtil.fixup(ipicTargetImportDto.getValue()))) {
			messages.add("第[" + ipicTargetImportDto.getRow() + "]行请输入正确的目标值!");
		} else if (StringUtil.toDouble(StringUtil.fixup(ipicTargetImportDto.getValue())) <= 0) {
			messages.add("第[" + ipicTargetImportDto.getRow() + "]行请输入正确的目标值!");
		}
		if(ipicTargetImportDto.getSupplierId()!=null&&ipicTargetImportDto.getYear()!=null){
			//检查数据库是否存在
			if(ipicTargetList!=null&&ipicTargetList.size()>0){
				for (IpicTarget t:ipicTargetList) {
					if(t.getSupplierCode().endsWith(ipicTargetImportDto.getSupplierCode())&&t.getYear().endsWith(ipicTargetImportDto.getYear())){
						messages.add("第[" + ipicTargetImportDto.getRow() + "]行目标值数据库已存在!");
						break;
					}
				}
			}
		}
		if (repeat != null&&repeat.size()>0) {
			for (IpicTargetImportDto i:repeat) {
				if (ipicTargetImportDto.getYear().equals(i.getYear())&&ipicTargetImportDto.getSupplierCode().equals(i.getSupplierCode()) && !StringUtil.fixup(ipicTargetImportDto.getRow()).equals(StringUtil.fixup(i.getRow()))) {
					messages.add("第[" + ipicTargetImportDto.getRow() + "]行与" + "第[" + i.getRow() + "]行" + "供应商、年份重复!");
					break;
				}
			}
		}
		return messages;
	}
}
